package com.study.springbootsecurity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.springbootsecurity.constants.SysUserExceptionConstants;
import com.study.springbootsecurity.entity.Role;
import com.study.springbootsecurity.entity.SysUser;
import com.study.springbootsecurity.exception.BusinessException;
import com.study.springbootsecurity.mapper.SysUserMapper;
import com.study.springbootsecurity.service.RoleService;
import com.study.springbootsecurity.service.SysUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: TODO
 * @author: Huangjianyun
 * @date: 2025-06-03 19:43
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    private RoleService roleService;

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>().eq(StringUtils.isNotBlank(username), SysUser::getUsername, username);
        SysUser sysUser = this.baseMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new BusinessException(SysUserExceptionConstants.SYS_USER_NOT_EXITS);
        }
        List<Role> roleList = roleService.findByUserId(sysUser.getId());
        sysUser.setRoles(roleList);
        return sysUser;
    }

//    @Override
//    public String login() {
//
//        return "";
//    }
}
